import api from './api.js'
import { dateFormat } from './utils.js'

function render($el, data) {
  let html = ''
  for(let i = 0; i < data.length; i++) {
    const item = data[i]
    html += `
      <a href="/detail.html?id=${item.id}" class="list-group-item list-group-item-action flex-column align-items-start">
        <div class="d-flex w-100 justify-content-between">
          <h5 class="mb-1">${item.title}</h5>
          <small>${dateFormat(item.createtime)}</small>
        </div>
        <small>${item.author}</small>
      </a>
    `
  }
  $el.innerHTML = html
}

let result = api.getList()
result.then(res => {
  if (res.errno === 0) {
    let data = res.data
    render($('#list')[0], data)
    console.log(data)
  } else {
    console.log('res.errno:', res.errno)
  }
}).catch(err => {
  console.error(err)
})
